package org.dtrd.modules.organization.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.organization.entity.bean.DoctorOrganizationDetail;
import org.dtrd.modules.organization.entity.bean.DoctorOrganizationListInfo;
import org.dtrd.modules.organization.entity.po.DtrdRlDoctorOrganization;
import org.dtrd.modules.organization.entity.request.DoctorOrganizationListRequest;
import org.dtrd.modules.organization.entity.request.DoctorOrganizationPageRequest;
import org.dtrd.modules.organization.mapper.DtrdRlDoctorOrganizationMapper;
import org.dtrd.modules.organization.service.IDtrdRlDoctorOrganizationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 机构和医生关系表 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-14
 */
@DS("multi-datasource1")
@Service
public class DtrdRlDoctorOrganizationServiceImpl extends ServiceImpl<DtrdRlDoctorOrganizationMapper, DtrdRlDoctorOrganization> implements IDtrdRlDoctorOrganizationService {

    private DtrdRlDoctorOrganizationMapper dtrdRlDoctorOrganizationMapper;

    @Override
    public List<DoctorOrganizationListInfo> getDoctorOrganizationList(DoctorOrganizationListRequest request) {
        LambdaQueryWrapper<DtrdRlDoctorOrganization> queryWrapper = baseQueryWrapper();
        List<DtrdRlDoctorOrganization> poList = list(queryWrapper);
        List<DoctorOrganizationListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(DoctorOrganizationListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<DoctorOrganizationListInfo> pageDoctorOrganization(DoctorOrganizationPageRequest request) {
        LambdaQueryWrapper<DtrdRlDoctorOrganization> queryWrapper = baseQueryWrapper();
        IPage<DtrdRlDoctorOrganization> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(DoctorOrganizationListInfo::new);
    }

    @Override
    public DoctorOrganizationDetail getDoctorOrganization(Integer dataId) {
        DtrdRlDoctorOrganization dtrdRlDoctorOrganization = getById(dataId);
        return Optional.ofNullable(dtrdRlDoctorOrganization)
                        .map(po -> new DoctorOrganizationDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateDoctorOrganization(DoctorOrganizationDetail dto) {
        DtrdRlDoctorOrganization dtrdRlDoctorOrganization = new DtrdRlDoctorOrganization();
        if(dto.getDataId() == null){
            dtrdRlDoctorOrganization = dtrdRlDoctorOrganization.init();
        }
        dtrdRlDoctorOrganization.parseFromDto(dto);
        return saveOrUpdate(dtrdRlDoctorOrganization);
    }

    @Override
    public boolean removeDoctorOrganization(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public List<String> getDoctorRlOrgCode(Integer doctorId) {
        LambdaQueryWrapper<DtrdRlDoctorOrganization> queryWrapper = baseQueryWrapper()
                .eq(DtrdRlDoctorOrganization::getDoctorId, doctorId);
        List<DtrdRlDoctorOrganization> list = this.list(queryWrapper);
        List<String> orgCodes = new ArrayList<>(list.size());
        if (CollectionUtil.isNotEmpty(list)) {
            orgCodes = list.stream()
                    .map(DtrdRlDoctorOrganization::getOrgCode)
                    .collect(Collectors.toList());
        }
        return orgCodes;
    }

    private LambdaQueryWrapper<DtrdRlDoctorOrganization> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdRlDoctorOrganization.class)
                        .eq(DtrdRlDoctorOrganization::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdRlDoctorOrganization> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdRlDoctorOrganization.class)
                        .eq(DtrdRlDoctorOrganization::getIsDel, 0);
    }
}
